package com.dingreading.cloud.soms.util;

import java.io.BufferedReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

public class PhoneNumberUtils {

    /**
     * 逐行读取并过滤空行
     */
    public static List<String> splitLinesNoEmptyWithReader(String text) {
        List<String> lines = new ArrayList<>();

        if (text == null || text.isEmpty()) {
            return lines;
        }

        try (BufferedReader reader = new BufferedReader(new StringReader(text))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if (!line.trim().isEmpty()) {
                    String s = cleanPhoneNumber(line);
                    lines.add(s);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return lines;
    }


    /**
     * 去除手机号码中的空格和常见特殊符号
     */
    public static String cleanPhoneNumber(String phoneNumber) {
        if (phoneNumber == null || phoneNumber.trim().isEmpty()) {
            return phoneNumber;
        }

        // 去除所有空格、连字符、点、括号、加号等常见分隔符
        return phoneNumber.replaceAll("[\\s\\-\\.\\(\\)\\+]", "");
    }

    public static void main(String[] args) {
        String text = "18919658225\n" +
                "13675518071\n" +
                "13956076530\n" +
                "18919668223";

        List<String> strings = splitLinesNoEmptyWithReader(text);
        for (String string : strings) {
            System.out.println("string = " + string);
        }
    }

}
